const db = wx.cloud.database()
Page({
  data: {
    organization_list: [],
    dataSum: null,
    wechart: "无",
    phone: "123"
  },
  goDetail(e) {
    wx.navigateTo({
      url: 'detail/detail?id=' + e.target.dataset.id,
    })
  },
  call(e) {
    var that = this;
    console.log(e.target.dataset.id)
    db.collection('organization').where({
      _id: e.target.dataset.id
    }).get({
      success: function (res) {
        that.setData({
          wechart: res.data[0].wechart,
          phone: res.data[0].phone,
        })
      }
    })
    wx.showModal({
      title: '联系社团',
      content: '请选择下方联系方式',
      showCancel: true, //是否显示取消按钮
      cancelText: "加微信号",
      confirmText: "拨打电话",
      success: function (res) {
        if (res.cancel) {
          wx.setClipboardData({
            data: that.data.wechart,
            success: function (res) {
              wx.getClipboardData({
                success: function (res) {
                  wx.showToast({
                    title: '复制成功'
                  })
                }
              })
            }
          })
        } else {
          //点击确定
          wx.makePhoneCall({
            phoneNumber: that.data.phone,
          })
        }
      },
      fail: function (res) {}, //接口调用失败的回调函数
      complete: function (res) {}, //接口调用结束的回调函数（调用成功、失败都会执行）
    })
  },
  goAdd() {
    wx.navigateTo({
      url: 'add/add',
    })
  },
  //数据库查询数据
  onLoad: function (e) {
    var that = this;
    wx.showLoading({
      title: '加载中...',
    })
    wx.showShareMenu({
      withShareTicket: true
    })
    db.collection("organization").where({
      show: 'yes'
    }).count({
      success: function (res) {
        console.log(res)
        //获取所有记录条数
        that.data.dataSum = res.total;
      }
    })
    db.collection('organization').where({
      show: 'yes'
    }).limit(6).orderBy('time', 'desc').get({
      success: function (res) {
        that.setData({
          organization_list: res.data,
        })
        wx.hideLoading({
          success: (res) => {},
        })
      },
      fail: function (event) {}
    })
  },
  //下拉刷新
  onPullDownRefresh: function () {
    const that = this;
    wx.showNavigationBarLoading()
    setTimeout(function () {
      that.onLoad();
      wx.hideNavigationBarLoading() //完成停止加载
      wx.stopPullDownRefresh() //停止下拉刷新
    }, 1000);
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    var that = this;
    var temp = [];
    if (that.data.organization_list.length < that.data.dataSum) {
      try {
        wx.showLoading({
          title: '加载中',
        })
        db.collection('organization').skip(that.data.organization_list.length).limit(6).orderBy('time', 'desc').where({
          show: 'yes'
        }).get({

          success: function (res) {

            if (res.data.length > 0) {
              for (var i = 0; i < res.data.length; i++) {
                var tempshopping = res.data[i];
                temp.push(tempshopping);
              }
              var dataSum = {};
              dataSum = that.data.organization_list.concat(temp);
              that.setData({
                organization_list: dataSum,
              })
            }
            wx.hideLoading();
          },
          fail: function (event) {
            console.log("======" + event);
          }
        })
      } catch (e) {
        console.error(e);
      }
    } else if (that.data.organization_list.length >= that.data.dataSum) {
      wx.showToast({
        title: '已为你加载全部',
        icon: 'none'
      })
    }
  }
})